package me.thinkjet.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import me.thinkjet.auth.AuthManager;
import me.thinkjet.auth.AuthRoles;
import me.thinkjet.model.*;

@ControllerBind(controllerKey = "/discuss", viewPath = "discuss")
public class DiscussController extends Controller {
	@Before(CacheInterceptor.class)
	@CacheName("discuss")
	public void index() {
		this.setAttr(
				"discuss",
				Discuss.dao.paginateByCache("discuss",
						"index_" + this.getParaToInt(1, 1),
						this.getParaToInt(1, 1), 10, "select d.*, r.*, u.username, u.name",
						"from discuss d left join discuss_record r on d.id=r.discuss_id " +
								"left join users u on u.id=d.author"));
	}

	@Before(CacheInterceptor.class)
	@CacheName("discuss")
	public void show() {
		this.setAttr("discuss", Discuss.dao.findById(this.getParaToLong()));
	}

	@AuthRoles()
	public void add() {

	}

	@AuthRoles()
	public void create() {
		Discuss discuss = getModel(Discuss.class);
		discuss.set("author", AuthManager.getSessionUser(this).getUser().getLong("id"));
		discuss.save();
		DiscussRecord.dao.set("discuss_id", discuss.getLong("id")).save();
		redirect("/discuss");
	}
}